package com.game.states;

import java.awt.AWTEvent;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

import com.game.system.Settings;
import com.game.system.Utils;
import com.jme.system.DisplaySystem;
import com.jmex.awt.swingui.JMEDesktopState;
import com.jmex.game.state.GameStateManager;

public class MenuState extends JMEDesktopState{
	private JInternalFrame modalDialog;
	private JPanel[] p;
	private JOptionPane[] o;
	private JComponent[] comp;
	private DisplaySystem display;
	private CurrentLevelState currentLevel;
	private int height, width;
	public static final int MAINMENU=0;
	public static final int GAMEMENU=1;
	public static final int OPTIONSMENU=2;
	public static final int EXITDIALOG=0;
	public static final int GRAPHICSDIALOG=1;
	public static final int BACKTOMAINDIALOG=2;
	private int origin;
	public MenuState(int width, int height){
		super(width, height);
		Toolkit.getDefaultToolkit().getSystemEventQueue().push(
	            new EventQueue() {
	            protected void dispatchEvent(AWTEvent event) {
	                if(! (event instanceof sun.awt.UngrabEvent)){
	                    super.dispatchEvent(event);
	                }
	             }
	        });
		this.height = height;
		this.width = width;
		Utils.setMouseVisible(true);
		p = new JPanel[3];
		setupDialogs();
		setupMainMenu();
		setupGameMenu();
		setupOptionsMenu();
		getDesktop().getJDesktop().add(p[0]);
		getDesktop().getJDesktop().updateUI();
	}
	private void setupDialogs() {
        //getDesktop().getJDesktop().add(modalDialog);
        o = new JOptionPane[3];
        JButton[] button = new JButton[o.length*2];
        for(int i=0; i<button.length; i++){
        	if(i%2!=0){
        		button[i] = new JButton("No");
        		button[i].addActionListener(new ActionListener(){
					public void actionPerformed(ActionEvent e) {
						hideDialog();
					}
        		});
        	}
        	else{
        		button[i] = new JButton("Yes");
        	}
        }
        button[0].addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
            	hideDialog();
            	System.exit(0);
			}
        });
        button[2].addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				hideDialog();
            	setCurrentMenu(MAINMENU);
            	int choice=((JComboBox)comp[1]).getSelectedIndex();
				Settings.getConfig().setProperty(Settings.TERRAIN_MAP_SIZE, ""+((int)Math.pow(2, 5+choice)));
				currentLevel.setActive(false);
			}
        });
        button[4].addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				hideDialog();
				setCurrentMenu(MAINMENU);
				currentLevel.setActive(false);
			}
        });
        o[0] = new JOptionPane("Are you sure you want to exit the game?",
        		JOptionPane.QUESTION_MESSAGE, 
        		JOptionPane.YES_NO_OPTION, null,
        		new JButton[]{button[0],button[1]});
        o[1] = new JOptionPane("To apply the graphics settings the game " +
        		"has to return to the main menu. Proceed?",
        		JOptionPane.WARNING_MESSAGE, 
        		JOptionPane.YES_NO_OPTION, null,
        		new JButton[]{button[2],button[3]});
        o[2] = new JOptionPane("Are you sure you want to return to the " +
        		"main menu? All unsaved progress will be lost",
        		JOptionPane.WARNING_MESSAGE, 
        		JOptionPane.YES_NO_OPTION, null,
        		new JButton[]{button[4],button[5]});
	}
	private void setupMainMenu() {
		p[0] = new JPanel();
		p[0].setOpaque(false);
		p[0].setLayout(new GridLayout(5,1));
		p[0].setSize(150,250);
		p[0].setLocation(width/2-p[0].getWidth()/2, height/2-p[0].getHeight()/2);
		JButton[] button = new JButton[5];
		button[0] = new JButton("Start Game");
		button[0].addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				setActive(false);
				currentLevel = new CurrentLevelState(MenuState.this);
				GameStateManager.getInstance().attachChild(currentLevel);
				currentLevel.setActive(true);
			}
		});
		button[1] = new JButton("Load");
		button[1].setEnabled(false);
		button[2] = new JButton("Multiplayer");
		button[2].setEnabled(false);
		button[3] = new JButton("Options");
		button[3].addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				setCurrentMenu(OPTIONSMENU);
				origin=MAINMENU;
			}
		});
		button[4] = new JButton("Exit");
		button[4].addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				showDialog(EXITDIALOG);
			}
		});
		int locy=50;
		for(int i=0; i<button.length; i++){
			button[i].setSize(150, locy);
			p[0].add(button[i]);
		}
	}
	private void setupGameMenu() {
		p[1] = new JPanel();
		p[1].setOpaque(false);
		p[1].setLayout(new GridLayout(6,1));
		p[1].setSize(150, 300);
		p[1].setLocation((width-p[1].getWidth())/2, (height-p[1].getHeight())/2);
		JButton button[] = new JButton[6];
		button[0] = new JButton("Resume Game");
		button[0].addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				currentLevel.resumeGame();
			}
		});
		button[1] = new JButton("Save");
		button[1].addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				System.out.println("wut");
			}
		});
		button[2] = new JButton("Load");
		button[2].addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				
			}
		});
		button[3] = new JButton("Options");
		button[3].addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				setCurrentMenu(OPTIONSMENU);
				origin=GAMEMENU;
			}
		});
		button[4] = new JButton("Return to Main Menu");
		button[4].addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				showDialog(BACKTOMAINDIALOG);
			}
		});
		button[5] = new JButton("Exit Game");
		button[5].addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				showDialog(EXITDIALOG);
			}
		});
		for(int i=0; i<button.length; i++){
			button[i].setSize(150, 50);
			p[1].add(button[i]);
		}
	}
	private void setupOptionsMenu() {
		p[2] = new JPanel();
		p[2].setOpaque(false);
		p[2].setLayout(null);
		p[2].setSize(width,height);
		JTabbedPane tabs = new JTabbedPane();
		tabs.setSize(320, 320);
		tabs.setLocation(width/2-tabs.getWidth()/2, height/2-tabs.getHeight()/2);
		//Graphics
		JPanel graphics = new JPanel();
		graphics.setLayout(null);
		String[][] items={{"Low","Medium","High"}};
		comp = new JComponent[2];
		int y=10;
		comp[0] = new JLabel("Terrain Quality");
		for(int i=0, j=0; i<comp.length; i++){
			if(i%2!=0){
				comp[i] = new JComboBox();
				comp[i].setLocation(150, y);
				comp[i].setSize(150,20);
				y+=30;
				for(int k=0; k<items[j].length; k++){
					if(items[j][k]==null) return;
					((JComboBox)comp[i]).addItem(items[j][k]);
					((JComboBox)comp[i]).setLightWeightPopupEnabled(true);
				}
				j++;
			}
			else{
				comp[i].setLocation(10, y);
				comp[i].setSize(90,20);
			}
			graphics.add(comp[i]);
		}
		JButton[] button = new JButton[2];
		button[0] = new JButton("Back");
		button[0].addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				setCurrentMenu(origin);
			}
		});
		button[1] = new JButton("Apply");
		button[1].addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				if(origin==GAMEMENU) showDialog(GRAPHICSDIALOG);
				else{
					int choice=((JComboBox)comp[1]).getSelectedIndex();
					Settings.getConfig().setProperty(Settings.TERRAIN_MAP_SIZE, ""+((int)Math.pow(2, 5+choice)));
				}
			}
		});
		for(int i=0; i<button.length; i++){
			button[i].setSize(100, 25);
			button[i].setLocation(tabs.getWidth()-120-i*(120), tabs.getHeight()-65);
			graphics.add(button[i]);
		}
		tabs.addTab("Graphics", graphics);
		p[2].add(tabs);
	}
	public void showDialog(int number){
        modalDialog = new JInternalFrame( "Dialog" );
        modalDialog.getContentPane().add(o[number]);
        getDesktop().setModalComponent(modalDialog);
        getDesktop().getJDesktop().add(modalDialog,0);
        modalDialog.setVisible(true);
        modalDialog.setSize(modalDialog.getPreferredSize());
        modalDialog.setLocation((width-modalDialog.getWidth())/2,
                (height-modalDialog.getHeight())/2);
        getDesktop().setFocusOwner(o[number]);
        getDesktop().getJDesktop().updateUI();
	}
	private void hideDialog(){
        modalDialog.setVisible(false);
        getDesktop().setModalComponent(null);
        getDesktop().getJDesktop().remove( modalDialog );
		getDesktop().getJDesktop().updateUI();
	}
	public void setCurrentMenu(int number){
		getDesktop().getJDesktop().removeAll();
		getDesktop().getJDesktop().add(p[number]);
		getDesktop().getJDesktop().updateUI();
	}
}